# ------------------------------------------
# 准备工作
# ------------------------------------------

setwd("/Users/eimei/Documents/Rdata/")  # 设定工作空间
data <- read.csv("HRdata.csv")  # 读取数据

# ------------------------------------------
# 概览数据
# ------------------------------------------

str(data)   # 使用str()进行数据概览
summary(data)   # 使用summary()进行数据概览

# ------------------------------------------
# 以行列方式选取数据框中的元素
# ------------------------------------------

data[1]             # 选取第1列
data[1:3]           # 选取第1列到第3列
data[1:5, 1:5]      # 选取第1行到第5行，第1列到第5列

data1 <- data[1:5, 1:5]      # 选取第5行以及倒数5列并赋值给data1
class(data1)        # 查看data1的数据结构类型

# ------------------------------------------
# 以变量名选取数据框中的元素
# ------------------------------------------

data[c("Gender", "JobInvolvement", "RelationshipSatisfaction")]

# ------------------------------------------
# 以“$”的方式选取数据框中的元素
# ------------------------------------------
data$Attrition <- factor(data$Attrition)
data$BusinessTravel <- factor(data$BusinessTravel, ordered = TRUE, 
                              levels = c("Non-Travel", "Travel_Rarely", "Travel_Frequently"))
data$Department <- factor(data$Department)
data$Education <- factor(data$Education, ordered = TRUE, 
                         levels = c("Primary education", "Secondary education", 
                                    "Junior college or vocational college", 
                                    "University", "Postdoctoral"))

# ------------------------------------------
# attach( ) 和 detach( )
# ------------------------------------------

attach(data)
data$OverTime <- factor(OverTime)
Over18 <- factor(Over18)
data$Over18 <- factor(OverTime)

summary(YearsAtCompany)

detach(data)

# ------------------------------------------
# with()
# ------------------------------------------

with(data, {
  stats <- summary(YearsAtCompany)
  stats
})

stats

with(data, {
  nokeepstats <- summary(YearsAtCompany)
  keepstats <<- summary(YearsAtCompany)
})

nokeepstats
keepstats

# ------------------------------------------
# 数据结构的转换练习
# ------------------------------------------
data2 <- as.matrix(data1)
data3 <- as.data.frame(data2)
str(data3)